package daoImpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Element;

import utils.XmlDefault;
import bean.Evenement;
import dao.EvenementDao;
import dao.SportDao;

public class EvenementDaoImpl extends XmlDefault implements EvenementDao {

	public EvenementDaoImpl(String nomFichier) {
		super(nomFichier);
	}
	public EvenementDaoImpl(){
		super("src/xml/evenement.xml");
	}
		
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Evenement getEvenement(int id) {
		List<Element> resultat = getRacine().getChildren("evenement");
		Iterator i = resultat.iterator();
		SportDao sportDao = new SportDaoImpl();
		while(i.hasNext()){
			Element courant = (Element)i.next();
			if(courant.getChild("id").getText().equals(String.valueOf(id)))
				return new Evenement(id,sportDao.getSport(Integer.parseInt(courant.getChildText("type"))));
		}
		return null;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Evenement> getAll() {
		List<Element> resultat = getRacine().getChildren("evenement");
		Iterator i = resultat.iterator();
		List<Evenement> evenement = new ArrayList<Evenement>();
		SportDao sportDao = new SportDaoImpl();
		while(i.hasNext()){
			Element courant = (Element)i.next();
			evenement.add(new Evenement(Integer.parseInt(courant.getChildText("id")),sportDao.getSport(Integer.parseInt(courant.getChildText("type")))));
		}
		return evenement;
		
	}
}
